#lang sicp

(define (sqrmod x mod)
  (remainder (* x x) mod))

(define (expmod base exp mod)
  (if (= exp 0)
      1
      (remainder (* (if (even? exp) 1 base) 
                    (expmod (sqrmod base mod) (quotient exp 2) mod)) mod)))

(define (fermat-test a)
  (define (check x)
    (= (expmod x a a) x))
  (check (+ 1 (random (- a 1)))))

(define (prime? a times)
  (if (= times 0) 
      #t
      (and (fermat-test a) (prime? a (- times 1)))))

(prime? 7 5)